{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Copyright 2014 Brett Slatkin, Pearson Education Inc.\n",
    "#\n",
    "# Licensed under the Apache License, Version 2.0 (the \"License\");\n",
    "# you may not use this file except in compliance with the License.\n",
    "# You may obtain a copy of the License at\n",
    "#\n",
    "#     http://www.apache.org/licenses/LICENSE-2.0\n",
    "#\n",
    "# Unless required by applicable law or agreed to in writing, software\n",
    "# distributed under the License is distributed on an \"AS IS\" BASIS,\n",
    "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
    "# See the License for the specific language governing permissions and\n",
    "# limitations under the License.\n",
    "\n",
    "# Preamble to mimick book environment\n",
    "import logging\n",
    "from pprint import pprint\n",
    "from sys import stdout as STDOUT"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Simple but a bit dense and memoty inefficient!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Example 1\n",
    "def index_words(text):\n",
    "    result = []\n",
    "    if text:\n",
    "        result.append(0)\n",
    "    for index, letter in enumerate(text):\n",
    "        if letter == ' ':\n",
    "            result.append(index + 1)\n",
    "    return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0, 5, 11]\n"
     ]
    }
   ],
   "source": [
    "# Example 2\n",
    "address = 'Four score and seven years ago...'\n",
    "address = 'Four score and seven years ago our fathers brought forth on this continent a new nation, conceived in liberty, and dedicated to the proposition that all men are created equal.'\n",
    "result = index_words(address)\n",
    "print(result[:3])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Use Generators, functions that use yield expressions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Example 3\n",
    "def index_words_iter(text):\n",
    "    if text:\n",
    "        yield 0\n",
    "    for index, letter in enumerate(text):\n",
    "        if letter == ' ':\n",
    "            yield index + 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "### list? Think of memory!!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0, 5, 11]\n"
     ]
    }
   ],
   "source": [
    "# Example 4\n",
    "result = list(index_words_iter(address))\n",
    "print(result[:3])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### yields outputs\tone\tword\tat\ta\ttime. Memory efficient. BEST!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Example 5\n",
    "def index_file(handle):\n",
    "    offset = 0\n",
    "    for line in handle:\n",
    "        if line:\n",
    "            yield offset\n",
    "        for letter in line:\n",
    "            offset += 1\n",
    "            if letter == ' ':\n",
    "                yield offset"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0, 5, 11]\n"
     ]
    }
   ],
   "source": [
    "# Example 6\n",
    "address_lines = \"\"\"Four score and seven years\n",
    "ago our fathers brought forth on this\n",
    "continent a new nation, conceived in liberty,\n",
    "and dedicated to the proposition that all men\n",
    "are created equal.\"\"\"\n",
    "\n",
    "with open('address.txt', 'w') as f:\n",
    "    f.write(address_lines)\n",
    "\n",
    "from itertools import islice\n",
    "with open('address.txt', 'r') as f:\n",
    "    it = index_file(f)\n",
    "    results = islice(it, 0, 3)\n",
    "    print(list(results))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
